package com.fairytask.ui
{
	import com.fairytask.data.RecordObject;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.engine.components.text.Text;
	import com.fairytask.engine.define.PackageType;
	import com.fairytask.engine.define.RecordDefine;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.TipsManage;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.TipsTools;
	import com.fairytask.ui.base.FrameUI;
	import com.fairytask.ui.main.TopUI;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	
	
	/**
	 * 充值vip同时获得 周奖励的界面 
	 * vip分为3种  1：月卡； 2：半年卡；  3：年卡
	 * @author yangj
	 * 
	 */	
	public class VipUI extends FrameUI
	{
		
		public static var _self	:VipUI;				//得到单例的模式
		//public var vipBg			:MovieClip;			//大的背景框
		public var vipLabel		:Label;				//顶部的vip字样
		public var vipBarBG		:MovieClip;			//进度条的背景显示的框
		public var vipBar			:MovieClip;			//进度条的显示
		public var vipAwardBtn		:Button;			//领取周奖励的按钮
		public var vipExplain		:Label;				//领取奖励每周一次的说明
		public var vipMBuyBg		:MovieClip;			//显示购买的背景
		public var vipHyBuyBg		:MovieClip;			
		public var vipYBuyBg		:MovieClip;
		public var vipMTExplain	:Text;				//vip类型文字
		public var vipHyTExplain	:Text;
		public var vipYTExplain	:Text;
		public var vipMText		:Text;				//vip不同类型的简介
		public var vipHyText		:Text;				
		public var vipYText		:Text;				
		public var vipMIcon		:Clip;				//vip图片
		public var vipHyIcon		:Clip;
		public var vipYIcon		:Clip;
		public var mButton			:Button;			//购买vip按钮
		public var hyButton		:Button;
		public var yButton			:Button;
		public var mVipBg			:MovieClip;			//vip简介的背景
		public var hyVipBg			:MovieClip;
		public var yVipBg			:MovieClip;
		
		
		
		private var _unbind		:Array	=	[];		//存放进行解绑数据的数组
		private var _vipKey		:Array	=	[];		//找到客户端vip配置表的关键字
		private var _tipStr		:String	="";		//用于显示tips的文字
		private var _vipLevel		:int;				//vip的等级
		private var _vipTypeKey	:Array	=	[];		//vip不同类型需要显示的不同的文字说明，同事还要去值进行显示
		
		
		
		/**
		 *得到单例的模式 
		 * @return 
		 * 
		 */		
		public static function getInstance():VipUI
		{
			if(_self == null)
			{
				_self = new VipUI();
			}
			return _self;
		}
		
		public function VipUI(popUI:Boolean=true)
		{
			super(popUI);
			init();
			initEvent();
		}
		
		/**
		 *初始化 
		 * 
		 */		
		private function init():void
		{
			_vipTypeKey = StaticIni.getIniVar("VipTypeList","Value").split(";");
			//给vip的添加不同的说明
			for(var i:int = 0;i<_vipTypeKey.length;i++)
			{
				var ary:Array	=	_vipTypeKey[i].split(",");
				addVipStr(i,ary);
			}
			
			//把关键字放入到数组中去
			_vipKey = StaticIni.getIniVar("VipRewardList","Value").split(",");
			vipYIcon.source = vipHyIcon.source = vipMIcon.source = ResConfig.pngPhotoPath +"14050002.jpg";
			mButton.data = 1;
			hyButton.data = 2;
			yButton.data = 3;
			// 给vip添加简介，图片旁边的
			for(var j:int=0;j<_vipKey.length;j++)
			{
				var money:int = int(StaticIni.getIniVar(_vipKey[j],"ConsumGold"));
				addVipMoney(j,money);
			}
		}
		
		
		/**
		 *初始化事件 
		 * 
		 */		
		private function initEvent():void
		{
			//绑定vip的表
			bindManager.BindRecord(model.scene,RecordDefine.VIPPRILEGEREC,onAddVip,onRemoveVip);
			mButton.addEventListener(MouseEvent.CLICK,sendBuyMessage);
			hyButton.addEventListener(MouseEvent.CLICK,sendBuyMessage);
			yButton.addEventListener(MouseEvent.CLICK,sendBuyMessage);
			vipAwardBtn.addEventListener(MouseEvent.CLICK,sendGetAwardMessage);
			vipAwardBtn.addEventListener(MouseEvent.ROLL_OVER,showTips);
			vipAwardBtn.addEventListener(MouseEvent.ROLL_OUT,hideTips);
		}
		
		/**
		 *绑定玩家vip等级 
		 * 
		 */		
		public function set PVipLevel(val:int):void
		{
			_vipLevel = val;
			getTipStr(val);
		}
		
		/**
		 *得到vip显示tip的文字 
		 * 
		 */		
		private function getTipStr(val:int):void
		{
			_tipStr = "";
			//当不是vip的时候显示一级vip要领取的奖励
			if(val==0)
			{
				val =1;
			}
			var key:String = _vipKey[val-1];
			var itemArray:Array = StaticIni.getIniVar(key,"WeekPrize").split(";");
			for(var i:int = 0;i<itemArray.length;i++)
			{
				if(itemArray[i]=="")
				{
					return;
				}
				var item:Array = itemArray[i].split(",");
				var configID:int = int(item[0]);
				var num:String = item[1];
				var name:String = onAddName(configID);
				_tipStr += name + "×" + num;
			}
			
		}
		
		/**
		 *得到对应的str 
		 * @param configID
		 * @return 
		 * 
		 */		
		private function onAddName(configID:int):String
		{
			var type:int = int(StaticIni.getIniVar(configID,"Type"));
			if(type == PackageType.EQUIP)
			{
				//根据不同的装备等级来显示不同的名字
				var level:int = int(StaticIni.getIniVar(configID,"Level"));
				var name:String = StaticIni.getIniVar(configID,"Name");
				var colorStr:int = int(StaticIni.getIniVar(configID,"Color"));
				var headName:String	= TipsTools.getRefineName(level) + name;
				var realName:String = TipsTools.onlyColor(headName,colorStr);
				return realName;
			}
			else
			{
				return StaticIni.getIniVar(configID,"Name");
			}
			
			
		}
		
		/**
		 *添加表行的记录 
		 * @param key
		 * @param val
		 * 
		 */		
		private function onAddVip(key:String,val:RecordObject):void
		{
			var type:int = val.GetProperties("0");
			//类型为1就代表是周奖励的类型
			if(type==1)
			{
				_unbind.push([val,"2",bindManager.BindRecordProp(val,"2",changLeftTime,[key])]);
			}	
		}
		
		/**
		 *修改周奖励类型的时间数据 
		 * 
		 */		
		private function changLeftTime(key:String,val:int,pList:Array):void
		{
			changBar(val);
		}
		
		/**
		 *删除表行的记录 
		 * @param key
		 * @param val
		 * 
		 */		
		private function onRemoveVip(key:String,val:RecordObject):void
		{
			var type:int = val.GetProperties("0");
			if(type==1)
			{
				for each(var itemBind:Array in _unbind)
				{
					bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
				}	
			}
		}
		
		
		/**
		 *改变进度条的长度，要求精确到天就行了 
		 * @param val
		 * 
		 */		
		private function changBar(val:int):void
		{
			if(val>7||val<0)
			{
				return;
			}
			if(val == 0)
			{
				vipBarBG.width = 0;
			}
			vipBarBG.width = vipBar.width/7*val;
			TopUI.getInstance().timeLineVip.stop();
			TopUI.getInstance().vipBtn.filters = null;
			if(val==7)
			{
				TopUI.getInstance().timeLineVip.play();
			}
		}
		
		/**
		 *点击发送购买的消息 
		 * 
		 */		
		private function sendBuyMessage(evt:MouseEvent):void
		{
			var type:int	=	evt.currentTarget.data;
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_BUY_VIP,type);
		}
		
		/**
		 *点击发送领取周奖励的的信息 
		 * 类型1 为日奖励，类型2为周奖励
		 * @param evt
		 * 
		 */		
		private function sendGetAwardMessage(evt:MouseEvent):void
		{
			//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_GET_VIP_PRIZE,2);
		}
		
		/**
		 *显示tips 
		 * 
		 */		
		private function showTips(evt:MouseEvent):void
		{
			TipsManage.showTips(StaticIni.getSwfLangStrVar("1000000661",[_tipStr]));
		}
		
		/**
		 *隐藏tips 
		 * 
		 */		
		private function hideTips(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
		
		/**
		 *判断不同的vip和显示的数组 
		 * @param val
		 * @param ary
		 * 
		 */		
		private function addVipStr(val:int,ary:Array):void
		{
			switch(val)
			{
				case 0:
					showVipExplain(vipMText,ary);
					break;
				case 1:
					showVipExplain(vipHyText,ary);
					break;
				case 2:
					showVipExplain(vipYText,ary);
					break;
			}
		}
		
		/**
		 *显示vip不同类型的说明 
		 * 
		 */		
		private function showVipExplain(vip:Text,ary:Array):void
		{
			vip.htmlStr = "";
			for(var i:int= 0;i<ary.length;i++)
			{
				var type:int = int(StaticIni.getIniVar(ary[i],"Type"));
				var description:String = StaticIni.getIniVar(ary[i],"TypeDescription");
				var num:int = int(StaticIni.getIniVar(ary[i],"Privilege"));
				vip.htmlStr += retDiffentStr(type,description,num) +"<br>";
			}
			
		}
		
		/**
		 *返回不同的str，可能有些是要添加参数的 
		 * 
		 */		
		private function retDiffentStr(type:int,desc:String,num:int):String
		{
			var str:String = "";
			switch(type)
			{
				case 4:
				case 5:
				case 6:
					str = StaticIni.getSwfLangStrVar(desc,[num]);
					break;
				default:
					str = StaticIni.getSwfLangStr(desc);
			}
			return str;
		}
		
		/**
		 *把钱添加到对应的文字中去 
		 * @param val
		 * @param str
		 * 
		 */		
		private function addVipMoney(val:int,str:int):void
		{
			switch(val)
			{
				case 0:
					vipMTExplain.htmlStr = StaticIni.getSwfLangStrVar("1000000613",[str]);
					break;
				case 1:
					vipHyTExplain.htmlStr = StaticIni.getSwfLangStrVar("1000000614",[str]);
					break;
				case 2:
					vipYTExplain.htmlStr = StaticIni.getSwfLangStrVar("1000000615",[str]);
					break;
			}
		}
	}
}